package com.bw.product.server.service.impl;

import com.bw.product.common.domain.PmsSkuImages;
import com.bw.product.common.domain.PmsSkuInfo;
import com.bw.product.common.domain.PmsSpuInfoDesc;
import com.bw.product.server.mapper.*;
import com.bw.product.server.service.ItemInfoService;
import com.bw.product.server.vo.AttrGroupVo;
import com.bw.product.server.vo.ItemInfoVo;
import com.bw.product.server.vo.SaleAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemInfoServiceImpl implements ItemInfoService {

    @Autowired
    private PmsSkuInfoMapper pmsSkuInfoMapper;
    @Autowired
    private PmsAttrGroupMapper pmsAttrGroupMapper;
    @Autowired
    private PmsSkuSaleAttrValueMapper pmsSkuSaleAttrValueMapper;
    @Autowired
    private PmsSkuImagesMapper pmsSkuImagesMapper;
    @Autowired
    private PmsSpuInfoDescMapper pmsSpuInfoDescMapper;
    @Autowired
    private ThreadPoolExecutor itemThreadPoolExcuter;


    @Override
    public ItemInfoVo selectSkuItemInfoBySkuId(Long skuId) throws ExecutionException, InterruptedException {

        if (skuId == null) {
            throw new RuntimeException("参数异常");
        }

        ItemInfoVo itemInfoVo = new ItemInfoVo();

//     * 1.查sku详情   0.5s
        CompletableFuture<PmsSkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            PmsSkuInfo pmsSkuInfo = pmsSkuInfoMapper.selectPmsSkuInfoBySkuId(skuId);
            itemInfoVo.setSkuInfo(pmsSkuInfo);
            return pmsSkuInfo;
        }, itemThreadPoolExcuter);

//     * 2.查sku图片信息 1s
        CompletableFuture<Void> skuImagesCompletableFuture = CompletableFuture.runAsync(() -> {
            PmsSkuImages SkuImages = new PmsSkuImages();
            SkuImages.setSkuId(skuId);
            List<PmsSkuImages> pmsSkuImages = pmsSkuImagesMapper.selectPmsSkuImagesList(SkuImages);
            itemInfoVo.setSkuImagesList(pmsSkuImages);
        }, itemThreadPoolExcuter);

//     * 3.查spu相关信息销售属性 1s
        CompletableFuture<Void> skuSaleCompletableFuture = CompletableFuture.runAsync(() -> {
            List<SaleAttrVo> saleAttrVos = pmsSkuSaleAttrValueMapper.selectSpuSale(skuId);
            itemInfoVo.setSaleAttrs(saleAttrVos);
        }, itemThreadPoolExcuter);

//     * 4.查spu商品介绍 1s
        CompletableFuture<Void> skuPmsSkuInfoDescCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(pmsSkuInfo -> {
            if (pmsSkuInfo.getSpuId() == null) {
                throw new RuntimeException("参数异常");
            }
            PmsSpuInfoDesc pmsSpuInfoDesc = pmsSpuInfoDescMapper.selectPmsSpuInfoDescBySpuId(pmsSkuInfo.getSpuId());
            itemInfoVo.setSpuInfoDesc(pmsSpuInfoDesc);
        }, itemThreadPoolExcuter);

//     * 5.查规格和包装 05s
        CompletableFuture<Void> attrGroupCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(pmsSkuInfo -> {
            if (pmsSkuInfo.getSpuId() == null) {
                throw new RuntimeException("参数异常");
            }
            List<AttrGroupVo> attrGroupVos = pmsAttrGroupMapper.selectAttrGroupBySpuId(pmsSkuInfo.getSpuId());
            itemInfoVo.setAttrGroupVos(attrGroupVos);
        }, itemThreadPoolExcuter);

        CompletableFuture.allOf(skuInfoCompletableFuture, skuImagesCompletableFuture, skuSaleCompletableFuture, skuPmsSkuInfoDescCompletableFuture, attrGroupCompletableFuture).get();

        return itemInfoVo;

    }
}
